package com.linkoog.devtools.code_generator.setting.configurable;

import com.linkoog.devtools.code_generator.setting.configurable.ui.TemplateSettingPanel;
import com.linkoog.devtools.code_generator.setting.persistent.CodeGenDevToolsPersistent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nls.Capitalization;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

public class TemplateSettingConfigurable implements Configurable {

    private final CodeGenDevToolsPersistent apiCodeDevToolsPersistent;

    private TemplateSettingPanel templateSettingPanel;

    public TemplateSettingConfigurable(){
        this.apiCodeDevToolsPersistent = ServiceManager.getService(CodeGenDevToolsPersistent.class);
    }

    @Override
    public @Nls(capitalization = Capitalization.Title) String getDisplayName() {
        return "Template Setting";
    }

    @Override
    public @Nullable JComponent createComponent() {
        if (templateSettingPanel == null){
            templateSettingPanel = new TemplateSettingPanel();
        }
        return templateSettingPanel.getMyMainPanel();
    }


    @Override
    public boolean isModified() {
        return ! apiCodeDevToolsPersistent.getTemplateDataList().equals(templateSettingPanel.getTemplateDataList());
    }

    @Override
    public void apply() throws ConfigurationException {
        apiCodeDevToolsPersistent.setTemplateDataList(templateSettingPanel.getTemplateDataList());
    }

    /*@Override
    public void reset() {
        typeMappingPanel.getTypeMappingEditor().reset(apiCodeDevToolsPersistent.getTypeMappings());
    }*/

}
